<?php
/**
 * Auction:Xml.php
 * @author qqinxl
 * Sun Sep 30 08:30:00 GMT 2007
 * 
 * @category   Auction
 * @package  
 * @copyright  Copyright (c) qqinxl
 */
class File_Xml {
	/**
	 * 文件名
	 *
	 * @var string
	 */
	private $_xmlFile ;
	/**
	 * 读取的xml文件内容
	 *
	 * @var simplexml_load_file
	 */
	private $_xmlData ;
	/**
	 * 读取的xml文件内容(simplexml_load_file 转化为数组形式)
	 *
	 * @var array
	 */
	private $_xmlArray ;
	/**
	 * Enter description here...
	 *
	 * @param string $xmlFile
	 */
	public function __construct ( $xmlFile = '' ) {
		$this->setXml ( $xmlFile ) ;
		$this->parseXml () ;
	}
	/**
	 * 设置文件 并读取
	 *
	 * @param string $xmlFile
	 * @return void
	 */
	public function setXml ( $xmlFile = '' ) {		
		if (! empty ( $xmlFile )) {
			$this->_xmlFile = $xmlFile ;
		}
	}
	/**
	 * 读取文件内容
	 *
	 * @param string $xmlFile
	 * @return array
	 */
	public function parseXml () {
		try{			
			if(empty($this->_xmlFile)){
				throw new Exception('xml File is null.');
			}
			$result = simplexml_load_file ( $this->_xmlFile ) ;
			//$result  =  new SimpleXMLElement($this->_xmlFile);
			if (false !== $result) {
				$this->_xmlData = $result ;
				$this->_xmlArray = $this->xmlToArray ( $result ) ;
				return $this->_xmlArray ;
			} 
		}catch (Exception $e){
			throw $e;
		}
		return false ;
	}
	/**
	 * 将simplexml_load_file读取的内容转化为数组
	 *
	 * @param object $object
	 * @return array
	 */
	public function xmlToArray ( $object ) {
		try{
			if (is_array ( $object )) {
				$return = array();
				foreach ( $object as $key => $value ) {
					$return [ $key ] = $this->xmlToArray ( $value ) ;
				}
				return $return;
			} else {
				$var = get_object_vars ( $object ) ;
				if ($var) {
					$return = array();
					foreach ( $var as $key => $value ) {
						$return [ $key ] = ($key && ! $value) ? NULL : $this->xmlToArray ( $value ) ;
					}
					return $return;
				}
			}			
		}catch (Exception $e){
			throw $e;
		}
		return $object ;
	}
	/**
	 * 修改文件中的某一节点
	 *
	 * @param string $node
	 * @param string $value
	 * @return void
	 */
	public function modifyXml ( $node , $value ) {
		$node = ltrim ( $node, '/' ) ;
		$node = rtrim ( $node, '/' ) ;
		$node = str_replace ( '--', '/', $node ) ;
		$node = str_replace ( '->', '/', $node ) ;
		$_nodes = explode ( "/", $node ) ;
		$_counts = count ( $_nodes ) ;
		if ($_counts == 1) {
			$this->_xmlData->$node = $value ;
			file_put_contents ( $this->_xmlFile, $this->_xmlData->asXML () ) ;
			return ;
		}
		//$_key = $_nodes [ $_counts - 1 ] ;
		$i = $_counts ;
		$_XMLElementArray = array ( ) ;
		$_XMLElementArray [ $i ] [ 'data' ] = $this->_xmlData ;
		$_XMLElementArray [ $i ] [ 'name' ] = '' ;
		$i -- ;
		foreach ( $_nodes as $node ) {
			if (! empty ( $node )) {
				$_XMLElement = $_XMLElementArray [ $i + 1 ] [ 'data' ]->$node ;
				if (false === $_XMLElement) {
					throw new Exception ( 'Node Error.' ) ;
				}
				$_XMLElementArray [ $i ] [ 'data' ] = $_XMLElement ;
				$_XMLElementArray [ $i ] [ 'name' ] = $node ;
				
				$i -- ;
			}
		}
		$_XMLElementArray [ 0 ] [ 'data' ] = $value ;
		$node = $_XMLElementArray [ 0 ] [ 'name' ] ;
		$_XMLElementArray [ 1 ] [ 'data' ]->$node = $value ;
		file_put_contents ( $this->_xmlFile, $this->_xmlData->asXML () ) ;
	}

}
